<?php


class AbstractApp {

	var $errors = null;
	var $language = null;
    var $logger = null;
    
    var $app = null;
    var $entity = null;
    var $method = null;
    
    var $appId = null;
    var $entityId = null;
    var $methodId = null;
    
    var $context = null;
    
	public function __construct() {
		import("mvc.Context");
		import("util.logging.Logger");
		if (defined("LOGS_PATH"))
			$this->logger = new Logger(LOGS_PATH);
	}
	
	/**
	* @return void
	* @param Language $language
	* @desc Set current Language object
	*/
	function setLanguage(&$value) {
		$this->language = &$value;
	}
	
	/**
	* @return Language
	* @desc Retrieve current Language object
	*/
	function getLanguage() {
		return $this->language;
	}
	
	function setErrors(&$errors) {
		$this->errors = &$errors;
	}
	
	function &getErrors() {
		return $this->errors;
	}
	
	/**
	*
	*/
	function setContext(&$context) {
		$this->context = &$context;
	}
	
	/**
	*
	*/
	function &getContext() {
		return $this->context;
	}
	
	function setProperty($propertiesName, &$value) {
		$this->context->setProperty($propertiesName, &$value);
	}
	function &getProperty($propertiesName) {
		return $this->context->getProperty($propertiesName);
	}
	
	function parseParameters() {
		// 3 and more
		return array("entityId"=>3);
	}
	
	/**
	* @deprecated
	*/
	function init($request, $response, $smarty) {
	}
	
	/**
	* @return void
	* @param Array $app
	* @param Array $entity
	* @param Array $action
	* @param Object $context
	* @desc Initialize class
	*/
	function initialize($app, $entity, $action, &$context) {
		$this->app = $app;
    	$this->entity = $entity;
    	$this->method = $action;
    	
		$this->appId = $app['id'];
    	$this->entityId = $entity['id'];
    	$this->methodId = $action['id'];

    	$this->setContext(&$context);
    	
    	$map = $this->parseParameters();
    	if(isset($_GET["route"]) && strlen($_GET["route"])>0) {
			$arr = split("/",$_GET["route"]);
			if ($map && is_array($map))
			foreach ($map as $name=>$index) {
				if (isset($arr[$index])) $this->setProperty($name,$arr[$index]);
			}
		}
    	
		$iblocks = $context->config->getIBlocks();
		$iblocks = $context->config->getEntityIBlocks($this->appId, $this->entityId, $iblocks);
		try {
			foreach ($iblocks as $iblock) {
				$clazz = import((string)$iblock["class"]);
				if ($clazz) {
					$block = new $clazz();
					$block->execute(&$this, &$context);
				}
			}
		} catch (Exception $e) {
			echo 'Caught exception: ',  $e->getMessage(), "\n";
		}
	}
	
	/**
	* @return AppForward
	* @param unknown $entity
	* @param unknown $action
	* @param unknown $request
	* @param unknown $response
	* @param unknown $smarty
	* @desc Main method of runnig application
	*/
	public function execute(&$mapping, &$request, &$response, &$smarty) {
		
	}
	
	public function executeMethod($method, &$mapping, &$request, &$response, &$smarty) {
		// TODO: remote line, temporary for old version compatible
		$this->init(&$request, &$response, &$smarty);
		
		return $this->$method(&$mapping, &$request, &$response, &$smarty);
	}
	
	
	
	
	function getAdditionalParams() {
		if (isset($_GET["route"]) && strlen($_GET["route"])>0) {
			return split("/",$_GET["route"]);
		}
		return array();
	}
}
?>